import datetime
import tushare
import tushare as ts
import smtplib #smtp服务器
from email.mime.text import MIMEText #邮件文本

print(tushare.__version__)
 
pro = ts.pro_api('15ceb9f0738a78141878b3a3a22beed3e64c2b67ec2efd3d857f4ed0')


#邮件构建
def 网易邮件发送(subject,body):
    subject = subject #邮件标题
    sender = "liangchongwei214@163.com"#发送方
    content = body
    recver = "51291677@qq.com"#接收方
    password = "NCJUNXDLGZRNCDRW" #邮箱密码
    message = MIMEText(content,"plain","utf-8")
    #content 发送内容     "plain"文本格式   utf-8 编码格式

    message['Subject'] = subject #邮件标题
    message['To'] = recver #收件人
    message['From'] = sender #发件人

    smtp = smtplib.SMTP_SSL("smtp.163.com",994) #实例化smtp服务器
    smtp.login(sender,password)#发件人登录
    smtp.sendmail(sender,[recver],message.as_string()) #as_string 对 message 的消息进行了封装
    smtp.close()
 


#####计算均线涨跌
def UpDownVal(arr):
    ma5=arr["ma5"][0]
    ma5Last=arr["ma5"][1]
    ma10=arr["ma10"][0]
    ma10Last=arr["ma10"][1]
    val = "WAIT"
    if(ma5>ma10 and  ma5>ma5Last and ma10>ma10Last) :
        val = "BUY"
    if(ma5<ma10 and  ma5<ma5Last and ma10<ma10Last) :
        val = "SELL"       
    return val
#####函数结束

######分析信息 codeStr 代码 buyPrice 买入价格 info 备注
def CodeInfo(codeStr,buyPrice,info):
    time5 =( datetime.datetime.now()+ datetime.timedelta(days=-60)).strftime('%Y-%m-%d')
    #获取数据
    dp月 = ts.get_hist_data(codeStr,ktype='M',start=time5) 
    # print(dp月)  
    dp周 = ts.get_hist_data(codeStr,ktype='W',start=time5)    
    dp日 = ts.get_hist_data(codeStr,ktype='D',start=time5)  
    dp分60 = ts.get_hist_data(codeStr,ktype='60',start=time5)  
    dp分30 = ts.get_hist_data(codeStr,ktype='30',start=time5) 
    dp分15 = ts.get_hist_data(codeStr,ktype='15',start=time5) 
    dp分5 = ts.get_hist_data(codeStr,ktype='5',start=time5) 

    ##计算
    BS月 = UpDownVal(dp月)
    BS周 = UpDownVal(dp周)
    BS日 = UpDownVal(dp日)
    BS60 = UpDownVal(dp分60)
    BS30 = UpDownVal(dp分30)
    BS15 = UpDownVal(dp分15)
    BS5 = UpDownVal(dp分5)

    cost=""
    if(buyPrice !=0):
        cost = str(round(dp日["close"][0]-buyPrice,2))

    info = info + str(round(dp日["close"][0],2))
    ResultStr =info + " \r\n月_" + BS月 +" \r\n周_" + BS周 +" \r\n日_" + BS日 + " \r\n60分_" +BS60  
    ResultStr = ResultStr + " \r\n30分_" +BS30  + " \r\n15分_" +BS15  + " \r\n5分_" +BS5 
    ResultStr = ResultStr + " \r\n当前_" + cost
    ResultStr = ResultStr.replace("本接口即将停止更新","")

    # print(ResultStr)
    # SendmailStr("info",ResultStr)
    return ResultStr
#########结束


# SendmailStr("info","test")
 #大盘数据
 #
info = CodeInfo('sh000001',0,'上证指数_')
info = info+ CodeInfo('sz399001',0,'深圳指数_')

网易邮件发送("tushare",info)
# 网易邮件发送("tushare", " 月_\r\n 周_\r\n 日_\r\n 60分_\r\n"  )

# result = info.replace("本接口即将停止更新，请尽快使用Pro版接口：https://waditu.com/document/2","")


# info = info+ CodeInfo('601567',8.45,'三星医疗_') #三星医疗
# info = info+ CodeInfo('601398',5.51,'工商银行_') #工商银行
# info = info+ CodeInfo('600025',3.95,'华能水电_') #华能水电
#info = info+ CodeInfo('110022',2.7680,'易基消费') #易基消费
#info = info+ CodeInfo('515070',1.194,'AI智能') #AI智能

# mail.SendmailStr("上证",info)
#基金
#df = pro.fund_basic(market='E')
 